<?php
/**
 * //w/w/w.s/h/u/o/t//u/p/u c/o/m  说/图/谱/源/码/分/享
 */
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/12 0008
 * Time: 17:07
 */
defined("IN_IA")or exit("Access Denied");
checklogin();  //验证是否登录
global $_W,$_GPC;
$uniacid=$_W['uniacid'];
$op=$_GPC['op'] ? $_GPC['op'] : "index";

if($op=='index'){
	$condition=array();
    $condition['uniacid']=$uniacid;
    $condition['slide_type']=1;
    $listCount=pdo_getall("cqkundian_farm_slide",$condition);
    $total=count($listCount);   //数据的总条数
    $pageSize=10; //每页显示的数据条数
    $pageIndex=intval($_GPC['page']) ? intval($_GPC['page']) :1;  //当前页
    $pager=pagination($total,$pageIndex,$pageSize);
    $list=pdo_getall("cqkundian_farm_slide",$condition,'','','rank asc',array($pageIndex,$pageSize));
	include $this->template("web/slide/index");
}

if($op=='edit'){
	if($_GPC['id']){
		$list=pdo_get('cqkundian_farm_slide',array('id'=>$_GPC['id'],'uniacid'=>$uniacid));
	}
	include $this->template("web/slide/edit");
}

if($op=='saveModel'){
	$data=array(
        'slide_src'=>tomedia($_GPC['slide_src']),
        'slide_type'=>1,
        'link_type'=>$_GPC['link_type'],
        'link_param'=>$_GPC['link_param'],
        'status'=>$_GPC['status'],
        'rank'=>$_GPC['rank'],
        'uniacid'=>$uniacid,
    );
    if(empty($_GPC['id'])){  //新增
        $request=pdo_insert("cqkundian_farm_slide",$data);
    }else{
        $condition=array(
            'id'=>$_GPC['id'],
            'uniacid'=>$uniacid,
        );
        $request=pdo_update("cqkundian_farm_slide",$data,$condition);
    }
    if($request){
        message("操作成功",$this->createWebUrl("slide"));
    }else {
        message("操作失败", '', 'warning');
    }
}

if($op=='statusChange'){
	$id=$_GPC['id'];
	$res=pdo_update('cqkundian_farm_slide',array('status'=>$_GPC['status']),array('id'=>$id,'uniacid'=>$uniacid));
	echo $res ? json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
}

if($op=='delete'){
	$id=$_GPC['id'];
	$res=pdo_delete('cqkundian_farm_slide',array('id'=>$id,'uniacid'=>$uniacid));
	echo $res ? json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
}

if($op=='selectLink'){
    $request=array();
    $select_id=$_GPC['select_id'];

    if($select_id==1){//农田选租
        $selectData=array();
    }elseif ($select_id==2){//家畜领养
        $selectData=pdo_getall('cqkundian_farm_animal',array('uniacid'=>$uniacid,'is_putaway'=>1));
    }elseif ($select_id==3){ //农场签到
        $selectData=array();
    }elseif ($select_id==4){    //农场直播
        $selectData=array();
    }elseif ($select_id==5){    //普通商城
        $selectData=pdo_getall('cqkundian_farm_goods',array('uniacid'=>$uniacid,'is_put_away'=>1));
    }elseif ($select_id==6){    //团购商城
        $selectData=pdo_getall('cqkundian_farm_group_goods',array('uniacid'=>$uniacid,'is_put_away'=>1));
    }elseif ($select_id==7){    //团购商城
        $selectData=pdo_getall('cqkundian_farm_integral_goods',array('uniacid'=>$uniacid,'is_put_away'=>1));
    }elseif ($select_id==8){  //资讯列表
        $selectData=pdo_getall('cqkundian_farm_article',array('uniacid'=>$uniacid));
    }
    if($_GPC['id']){
        $id=$_GPC['id'];
        $slideData=pdo_get('cqkundian_farm_slide',array('id'=>$id,'uniacid'=>$uniacid));
        for($i=0;$i<count($selectData);$i++){
            if($slideData['link_param']==$selectData[$i]['id']){
                $selectData[$i]['is_select']=1;
            }
        }
    }
    $request['selectData']=$selectData;
    echo json_encode($request);die;
}

if($op=='common_slide'){
    $condition=array();
    $condition['uniacid']=$uniacid;
    $condition['slide_type']=2;
    $listCount=pdo_getall("cqkundian_farm_slide",$condition);
    $total=count($listCount);   //数据的总条数
    $pageSize=10; //每页显示的数据条数
    $pageIndex=intval($_GPC['page']) ? intval($_GPC['page']) :1;  //当前页
    $pager=pagination($total,$pageIndex,$pageSize);
    $list=pdo_getall("cqkundian_farm_slide",$condition,'','','rank asc',array($pageIndex,$pageSize));
    include $this->template("web/slide/common_slide");
}

if($op=='common_edit'){
    if($_GPC['id']){
        $list=pdo_get('cqkundian_farm_slide',array('id'=>$_GPC['id'],'uniacid'=>$uniacid));
    }
    include $this->template("web/slide/common_edit");
}
if($op=='commonSaveModel'){
    $data=array(
        'slide_src'=>tomedia($_GPC['slide_src']),
        'slide_type'=>2,
        'link_type'=>$_GPC['link_type'],
        'link_param'=>$_GPC['link_param'],
        'status'=>$_GPC['status'],
        'rank'=>$_GPC['rank'],
        'uniacid'=>$uniacid,
    );
    if(empty($_GPC['id'])){  //新增
        $request=pdo_insert("cqkundian_farm_slide",$data);
    }else{
        $condition=array(
            'id'=>$_GPC['id'],
            'uniacid'=>$uniacid,
        );
        $request=pdo_update("cqkundian_farm_slide",$data,$condition);
    }
    if($request){
        $url=url('site/entry/slide',array('m'=>'kundian_farm','op'=>'common_slide'));
        message("操作成功",$url);
    }else {
        message("操作失败", '', 'warning');
    }
}

if($op=='integral_slide'){
    $condition=array();
    $condition['uniacid']=$uniacid;
    $condition['slide_type']=3;
    $listCount=pdo_getall("cqkundian_farm_slide",$condition);
    $total=count($listCount);   //数据的总条数
    $pageSize=10; //每页显示的数据条数
    $pageIndex=intval($_GPC['page']) ? intval($_GPC['page']) :1;  //当前页
    $pager=pagination($total,$pageIndex,$pageSize);
    $list=pdo_getall("cqkundian_farm_slide",$condition,'','','rank asc',array($pageIndex,$pageSize));
    include $this->template("web/slide/integral_slide");
}

if($op=='integral_edit'){
    if($_GPC['id']){
        $list=pdo_get('cqkundian_farm_slide',array('id'=>$_GPC['id'],'uniacid'=>$uniacid));
    }
    include $this->template("web/slide/integral_edit");
}
if($op=='integralSaveModel'){
    $data=array(
        'slide_src'=>tomedia($_GPC['slide_src']),
        'slide_type'=>3,
        'link_type'=>$_GPC['link_type'],
        'link_param'=>$_GPC['link_param'],
        'status'=>$_GPC['status'],
        'rank'=>$_GPC['rank'],
        'uniacid'=>$uniacid,
    );
    if(empty($_GPC['id'])){  //新增
        $request=pdo_insert("cqkundian_farm_slide",$data);
    }else{
        $condition=array(
            'id'=>$_GPC['id'],
            'uniacid'=>$uniacid,
        );
        $request=pdo_update("cqkundian_farm_slide",$data,$condition);
    }
    if($request){
        $url=url('site/entry/slide',array('m'=>'kundian_farm','op'=>'integral_slide'));
        message("操作成功",$url);
    }else {
        message("操作失败", '', 'warning');
    }
}

if($op=='getWeather'){
//    $host = "http://jisutqybmf.market.alicloudapi.com";
//    $path = "/weather/query";
//    $method = "GET";
//    $appcode = "b461ee517e744653b38145169bbb1dd9";
//    $headers = array();
//    array_push($headers, "Authorization:APPCODE " . $appcode);
//    $querys = "location=29.598205,106.498797";
//    $bodys = "";
//    $url = $host . $path . "?" . $querys;
//
//    $curl = curl_init();
//    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
//    curl_setopt($curl, CURLOPT_URL, $url);
//    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//    curl_setopt($curl, CURLOPT_FAILONERROR, false);
//    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//    curl_setopt($curl, CURLOPT_HEADER, true);
//    if (1 == strpos("$".$host, "https://"))
//    {
//        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
//        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
//    }

//    var_dump(curl_exec($curl));
    $result='HTTP/1.1 200 OK
Server: Tengine
Date: Tue, 22 May 2018 01:50:11 GMT
Content-Type: application/json; charset=UTF-8
Content-Length: 5300
Connection: keep-alive
Vary: Accept-Encoding
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET,POST,PUT,DELETE,HEAD,OPTIONS,PATCH
Access-Control-Allow-Headers: X-Requested-With,X-Sequence,X-Ca-Key,X-Ca-Secret,X-Ca-Version,X-Ca-Timestamp,X-Ca-Nonce,X-Ca-API-Key,X-Ca-Stage,X-Ca-Client-DeviceId,X-Ca-Client-AppId,X-Ca-Signature,X-Ca-Signature-Headers,X-Ca-Signature-Method,X-Forwarded-For,X-Ca-Date,X-Ca-Request-Mode,Authorization,Content-Type,Accept,Accept-Ranges,Cache-Control,Range,Content-MD5
Access-Control-Max-Age: 172800
X-Ca-Request-Id: 5CFF8291-B0EB-43A8-A31C-1C94FD976DF0
X-Powered-By: PHP/5.4.16
X-Ca-Error-Message: OK

{"status":"0","msg":"ok","result":{"city":"重庆","cityid":"31","citycode":"101040100","date":"2018-05-22","week":"星期二","weather":"雨","temp":"18","temphigh":"23","templow":"18","img":"301","humidity":"89","pressure":"983","windspeed":"8.5","winddirect":"北风","windpower":"2级","updatetime":"2018-05-22 09:10:00","index":[{"iname":"空调指数","ivalue":"较少开启","detail":"您将感到很舒适，一般不需要开启空调。"},{"iname":"运动指数","ivalue":"较不宜","detail":"有较强降水，建议您选择在室内进行健身休闲运动。"},{"iname":"紫外线指数","ivalue":"弱","detail":"紫外线强度较弱，建议出门前涂擦SPF在12-15之间、PA+的防晒护肤品。"},{"iname":"感冒指数","ivalue":"较易发","detail":"相对今天出现了较大幅度降温，较易发生感冒，体质较弱的朋友请注意适当防护。"},{"iname":"洗车指数","ivalue":"不宜","detail":"不宜洗车，未来24小时内有雨，如果在此期间洗车，雨水和路上的泥水可能会再次弄脏您的爱车。"},{"iname":"空气污染扩散指数","ivalue":"优","detail":"气象条件非常有利于空气污染物稀释、扩散和清除，可在室外正常活动。"},{"iname":"穿衣指数","ivalue":"热","detail":"天气热，建议着短裙、短裤、短薄外套、T恤等夏季服装。"}],"aqi":{"so2":"9","so224":"8","no2":"41","no224":"39","co":"0.920","co24":"0.870","o3":"81","o38":"122","o324":"126","pm10":"57","pm1024":"47","pm2_5":"34","pm2_524":"29","iso2":"4","ino2":"21","ico":"10","io3":"26","io38":"70","ipm10":"52","ipm2_5":"48","aqi":"70","primarypollutant":"O3","quality":"良","timepoint":"2018-05-19 21:00:00","aqiinfo":{"level":"二级","color":"#FFFF00","affect":"空气质量可接受，但某些污染物可能对极少数异常敏感人群健康有较弱影响","measure":"极少数异常敏感人群应减少户外活动"}},"daily":[{"date":"2018-05-22","week":"星期二","sunrise":"05:58","sunset":"19:43","night":{"weather":"小雨","templow":"18","img":"7","winddirect":"持续无风向","windpower":"微风"},"day":{"weather":"大雨","temphigh":"23","img":"9","winddirect":"持续无风向","windpower":"微风"}},{"date":"2018-05-23","week":"星期三","sunrise":"05:58","sunset":"19:44","night":{"weather":"阴","templow":"18","img":"2","winddirect":"持续无风向","windpower":"微风"},"day":{"weather":"阴","temphigh":"25","img":"2","winddirect":"持续无风向","windpower":"微风"}},{"date":"2018-05-24","week":"星期四","sunrise":"05:57","sunset":"19:44","night":{"weather":"阴","templow":"19","img":"2","winddirect":"持续无风向","windpower":"微风"},"day":{"weather":"多云","temphigh":"25","img":"1","winddirect":"持续无风向","windpower":"微风"}},{"date":"2018-05-25","week":"星期五","sunrise":"05:57","sunset":"19:45","night":{"weather":"阴","templow":"22","img":"2","winddirect":"持续无风向","windpower":"微风"},"day":{"weather":"阴","temphigh":"27","img":"2","winddirect":"持续无风向","windpower":"微风"}},{"date":"2018-05-26","week":"星期六","sunrise":"05:57","sunset":"19:45","night":{"weather":"阴","templow":"22","img":"2","winddirect":"持续无风向","windpower":"微风"},"day":{"weather":"阵雨","temphigh":"25","img":"3","winddirect":"持续无风向","windpower":"微风"}},{"date":"2018-05-27","week":"星期日","sunrise":"05:56","sunset":"19:46","night":{"weather":"阴","templow":"21","img":"2","winddirect":"持续无风向","windpower":"微风"},"day":{"weather":"阴","temphigh":"24","img":"2","winddirect":"持续无风向","windpower":"微风"}},{"date":"2018-05-28","week":"星期一","sunrise":"05:56","sunset":"19:47","night":{"weather":"阴","templow":"20","img":"2","winddirect":"持续无风向","windpower":"微风"},"day":{"weather":"阴","temphigh":"25","img":"2","winddirect":"持续无风向","windpower":"微风"}}],"hourly":[{"time":"9:00","weather":"多云","temp":"25","img":"1"},{"time":"10:00","weather":"小雨","temp":"27","img":"7"},{"time":"11:00","weather":"小雨","temp":"22","img":"7"},{"time":"12:00","weather":"小雨","temp":"22","img":"7"},{"time":"13:00","weather":"多云","temp":"23","img":"1"},{"time":"14:00","weather":"多云","temp":"23","img":"1"},{"time":"15:00","weather":"多云","temp":"22","img":"1"},{"time":"16:00","weather":"多云","temp":"22","img":"1"},{"time":"17:00","weather":"多云","temp":"21","img":"1"},{"time":"18:00","weather":"多云","temp":"20","img":"1"},{"time":"19:00","weather":"多云","temp":"20","img":"1"},{"time":"20:00","weather":"多云","temp":"19","img":"1"},{"time":"21:00","weather":"小雨","temp":"18","img":"7"},{"time":"22:00","weather":"小雨","temp":"18","img":"7"},{"time":"23:00","weather":"多云","temp":"17","img":"1"},{"time":"0:00","weather":"小雨","temp":"16","img":"7"},{"time":"1:00","weather":"多云","temp":"17","img":"1"},{"time":"2:00","weather":"多云","temp":"17","img":"1"},{"time":"3:00","weather":"多云","temp":"17","img":"1"},{"time":"4:00","weather":"多云","temp":"17","img":"1"},{"time":"5:00","weather":"多云","temp":"17","img":"1"},{"time":"6:00","weather":"多云","temp":"16","img":"1"},{"time":"7:00","weather":"多云","temp":"17","img":"1"},{"time":"8:00","weather":"多云","temp":"18","img":"1"}]}}';
// var_dump($result);
    $start=strpos($result,'{');
    $str=substr($result,$start);
    $weather=json_decode($str);
    if($weather->status==0){
        $msg=$weather->result;
        $data=array(
            'city'=>$msg->city,
            'week'=>$msg->week,
            'weather'=>$msg->weather,
            'temp'=>$msg->temp,
            'temphigh'=>$msg->temphigh,
            'templow'=>$msg->templow,
            'img'=>$msg->img,
            'humidity'=>$msg->humidity,
            'windpower'=>$msg->windpower,
            'quality'=>$msg->aqi->quality,
        );
        $daily=$msg->daily;
        $daily_arr=array();
        for ($i=0;$i<count($daily);$i++){
            $daily_arr[$i]['date']=$daily[$i]->date;
            $daily_arr[$i]['week']=$daily[$i]->week;
            $daily_arr[$i]['weather']=$daily[$i]->day->weather;
            $daily_arr[$i]['img']=$daily[$i]->day->img;
            $daily_arr[$i]['temphigh']=$daily[$i]->day->temphigh;
            $daily_arr[$i]['templow']=$daily[$i]->night->templow;
            $daily_arr[$i]['windpower']=$daily[$i]->day->windpower;
        }
        $data['daily']=$daily_arr;

        $hourly=$msg->hourly;
        $hourly_arr=array();
        for ($i=0;$i<count($hourly);$i++){
            $hourly_arr[$i]['time']=$hourly[$i]->time;
            $hourly_arr[$i]['weather']=$hourly[$i]->weather;
            $hourly_arr[$i]['temp']=$hourly[$i]->temp;
            $hourly_arr[$i]['img']=$hourly[$i]->img;
        }
        $data['hourly']=$hourly_arr;

        var_dump($data);
    }
}


